<?php
namespace Admin\Controller;
use Tool\AdminController;
use Think\Verify;

class ManagerController extends AdminController {
    function login() {
    	if (!empty($_POST)) {
    		$ver = new Verify();
    		if (! $ver->check($_POST['code'])) {
    			$error = '验证码错误';
    			$this->assign('error',$error);
    		} else {
    			$user = new \Model\ManagerModel();
    			$name = $_POST['name'];
    			$z = $user->where("name='$name'")->find();
    			if ($z) {
    				if (md5($_POST['pass']) == $z['password']) {
						session('id',$z['id']);
						session('name',$z['name']);
    					$this->redirect('Index/index');
    				}else {
    					$error = '密码错误，请重试';
    					$this->assign('error',$error);
    				}
    			} else {
    				$error = '管理员用户不存在';
    				$this->assign('error',$error);
    			}
    		}
    	}
    	$this->display();
    }
    
    function logout() {
    	session(null);
    	$this->redirect('login');
    }
    //验证码
    function verifyImg() {
    	$user = new \Model\UserModel();
    	$user->verify();
    }
    
    function addmanager() {
    	$manager = new \Model\ManagerModel();
    	$rol = D('Role')->select();
    	$this->assign('rol',$rol);
    	
    	if ($_GET['id'] != false) {
    		$ma = $manager->where('id=' . $_GET['id'])->find();
    		$this->assign('ma',$ma);
    		if (!empty($_POST)) {
    			if (!$manager->create()) {
    				$this->assign('error',$manager->getError());
    			} else {
    				$post = $manager->savemanager($_POST);
    				$z = $manager->where('id=' . $_GET['id'])->save($post);
    				if ($z) {
    					$this->redirect('Tips/tips');
    				}
    			}
    		}
    	} else {
    		if (!empty($_POST)) {
    			if (!$manager->create()) {
    				$this->assign('error',$manager->getError());
    			} else {
    				$post = $manager->savemanager($_POST);
    				$z = $manager->add($post);
    				if ($z) {
    					$this->redirect('Tips/tips');
    				}
    			}
    		}
    	}

    	include ('top.php');
    	$this->display();
    }

    function manager() {
    	include ('top.php');
    	$sql = 'select ts_manager.*,ts_role.role_name from ts_manager inner join ts_role on ts_manager.role=ts_role.role_id';
    	$manager = M('Role')->query($sql);
    	$this->assign('manager',$manager);
    	$this->display();
    }
    
    function del() {
    	$del = D('Manager')->where('id=' . $_GET['id'])->delete();
    	if ($del) {
    		$this->redirect('Tips/tips');
    	}
    }
}